package com.lv.phuotbacnam.ui.navigation;

import android.app.ActionBar;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lv.phuotbacnam.R;

/**
 * Action bar wrapper
 */
public class NavigationBar {

	/** Activity to build the navigation bar for */
	private Activity activity;

	/** Base view of the action bar */
	private View baseView;

	/** View layout resource id */
	private int layoutResourceId;

	public NavigationBar(Activity activity, int layoutResourceId) {
		this.activity = activity;
		this.layoutResourceId = layoutResourceId;
	}

	public void initialize() {
		baseView = View.inflate(activity, layoutResourceId, null);
		ActionBar actionBar = activity.getActionBar();
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayShowCustomEnabled(true);
		actionBar.setCustomView(baseView);
		actionBar.show();
	}

	public void setLeftAction(View view, View.OnClickListener onClickListener) {
		ViewGroup leftHolder = (ViewGroup) baseView
				.findViewById(R.id.action_left_holder);
		leftHolder.removeAllViews();
		leftHolder.addView(view);
		leftHolder.setOnClickListener(onClickListener);
	}

	public void setRightAction(View view, View.OnClickListener onClickListener) {
		ViewGroup rightHolder = (ViewGroup) baseView
				.findViewById(R.id.action_right_holder);
		rightHolder.removeAllViews();
		rightHolder.addView(view);
		rightHolder.setOnClickListener(onClickListener);
	}

	public void setLeftActionButton(int imageResourceId,
			View.OnClickListener onClickListener) {
		baseView.findViewById(R.id.action_left_holder).setOnClickListener(
				onClickListener);
		if (imageResourceId != 0) {
			ImageView view = (ImageView) baseView
					.findViewById(R.id.action_left);
			view.setImageResource(imageResourceId);
		}
	}

	public void setRightActionButton(int imageResourceId,
			View.OnClickListener onClickListener, boolean isFromBackActivity) {
		View action_right_holder = baseView
				.findViewById(R.id.action_right_holder);
		if (isFromBackActivity)
			action_right_holder.setVisibility(View.INVISIBLE);
		else {
			action_right_holder.setVisibility(View.VISIBLE);
			action_right_holder.setOnClickListener(onClickListener);
			if (imageResourceId != 0) {
				ImageView view = (ImageView) baseView
						.findViewById(R.id.action_right);
				view.setImageResource(imageResourceId);
			}
		}
	}

	public void setActionTitle(String title) {
		((TextView) baseView.findViewById(R.id.action_title)).setText(title);
	}

	public void show() {
		ActionBar actionBar = activity.getActionBar();
		if (actionBar != null) {
			actionBar.show();
		}
	}

	public void hide() {
		ActionBar actionBar = activity.getActionBar();
		if (actionBar != null) {
			activity.getActionBar().hide();
		}
	}
}
